package com.gitee.wsl.compose.chart.sample

import kotlinx.datetime.Clock
import kotlin.random.Random
import kotlin.time.Duration.Companion.days
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.ext.color.random
import com.gitee.wsl.compose.chart.Chart
import com.gitee.wsl.compose.chart.heatmap.ContributionData
import com.gitee.wsl.compose.chart.heatmap.ContributionHeatmapData
import com.gitee.wsl.compose.chart.heatmap.Heatmap
import com.gitee.wsl.compose.chart.heatmap.HeatmapRenderer

val heatmapSampleView = object : SampleView {
    override val name: String = "XY Line"

    override val thumbnail = @Composable {
        ThumbnailTheme {
            GithubGraph(color = Color.random())
        }
    }

    override val content: @Composable () -> Unit = @Composable {
        GithubGraph(color = Color.random())
    }
}


private fun getHeatmapRenderer(color: Color) =
    HeatmapRenderer(
        ContributionHeatmapData(
            baseColor = color,
            contributions =
                buildList {
                    val now = Clock.System.now()
                    repeat(365) { day ->
                        val date = now.minus(day.days)
                        val count = if (Random.nextFloat() > 0.6f) Random.nextInt(1, 15) else 0
                        add(ContributionData(date, count))
                    }
                },
        ),
    )

@Composable
fun GithubGraph(
    modifier: Modifier = Modifier,
    color: Color,
) {
    Chart.Heatmap(
        renderer = getHeatmapRenderer(color = color),
        modifier = Modifier.fillMaxWidth().height(112.dp),
    )
}
